/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import control.Controller;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import models.Var;

/**
 *
 * @author Béo
 */
public class viewCleaner extends GameCanvas {

    private Graphics g;
    private Image back1, back2, bannercleaner, clean, clean1;
    private long totalRam, freeRam;
    private String power;
    private Random rd = new Random();

    public long getTotalRam() {
        return totalRam;
    }

    public void setTotalRam(long totalRam) {
        this.totalRam = totalRam;
    }

    public long getFreeRam() {
        return freeRam;
    }

    public void setFreeRam(long freeRam) {
        this.freeRam = freeRam;
    }

    public viewCleaner() {
        super(false);
        g = getGraphics();
        try {
            back1 = Image.createImage(Var.BACK1);
            back2 = Image.createImage(Var.BACK2);
            clean = Image.createImage(Var.CLEAN);
            clean1 = Image.createImage(Var.CLEAN1);
            bannercleaner = Image.createImage(Var.CLEANER);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        freeRam = Runtime.getRuntime().freeMemory();
        totalRam = Runtime.getRuntime().totalMemory();
    }

    public void drawBackground() {
        g.setColor(Var.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(bannercleaner, getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP);
        g.drawImage(back1, getWidth() - 55, getHeight() - 50, Graphics.TOP | Graphics.LEFT);
        g.drawImage(clean, 5, getHeight() - 50, Graphics.TOP | Graphics.LEFT);
        g.setColor(Var.RED);
        g.drawString("RAM:", 15, 50, Graphics.LEFT | Graphics.BASELINE);
        g.setColor(Var.BLUE);
        g.drawString("Total: " + Long.toString(totalRam), 30, 95, Graphics.LEFT | Graphics.BASELINE);
        flushGraphics();
    }

    public void drawString() {
        power = System.getProperty("com.nokia.mid.batterylevel");
        int a = Integer.parseInt(power.substring(0, power.length() - 1));
        g.setColor(Var.BLACK);
        g.fillRect(30, 50, 170, 20);
        g.fillRect(30, 210, 170, 30);
        g.setColor(Var.BLUE);
        g.fillArc(80, 120, 80, 80, 0, 360);
        g.fillRect(30, 240, 180, 20);
        if (((float) freeRam / totalRam) < 0.3) {
            g.setColor(Var.RED);
        } else if (((float) freeRam / totalRam) <= 0.6) {
            g.setColor(Var.YELLOW);
        } else if (((float) freeRam / totalRam) > 0.6) {
            g.setColor(Var.GREEN);
        }
        g.drawString("Free: " + Long.toString(freeRam - 50 + rd.nextInt(100)), 30, 70, Graphics.LEFT | Graphics.BASELINE);
        g.fillArc(80, 120, 80, 80, 0, (int) ((1 - ((float) freeRam / totalRam)) * 360));
        if (a < 30) {
            g.setColor(Var.RED);
        } else if (a < 60) {
            g.setColor(Var.YELLOW);
        } else if (a > 60) {
            g.setColor(Var.GREEN);
        }
        g.drawString("Power: " + power, 30, 230, Graphics.LEFT | Graphics.BASELINE);
        g.fillRect(30, 240, (int) ((float) a * 1.8f), 20);
        flushGraphics();
    }

    public void draw() {
        drawBackground();
        Thread t = new Thread() {
            public void run() {
                while (true) {
                    drawString();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        };
        t.start();

    }

    public void pointerPressed(int x, int y) {
        if (x >= 5 && x <= 65 && y >= getHeight() - 50 && y <= getHeight()) {
            g.drawImage(clean1, 5, getHeight() - 50, Graphics.TOP | Graphics.LEFT);
            flushGraphics();
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            Controller.getInstance().showCleaning();
        }
        if (x >= getWidth() - 50 && x <= getWidth() && y >= getHeight() - 50 && y <= getHeight()) {
            g.drawImage(back2, getWidth() - 55, getHeight() - 50, Graphics.LEFT | Graphics.TOP);
            flushGraphics();
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            Controller.getInstance().showHome();
        }

    }

}
